package com.snail.readyvr.mvp.presenter;

import com.snail.readyvr.mvp.contract.ChangePswActivityContract;
import com.snail.readyvr.mvp.model.ChangePswParam;
import com.snail.readyvr.mvp.model.PhoneRegisterResponse;
import com.snail.readyvr.rest.HttpResultFunc;
import com.snail.readyvr.rest.LoginRestClient;
import com.snail.readyvr.rest.RestApiUtils;

import javax.inject.Inject;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by MRKING on 2016/11/10.
 */

public class ChangePswActivityPresenter implements ChangePswActivityContract.Presenter {

    private ChangePswActivityContract.View mView;
    private LoginRestClient mRestClient;

    @Inject
    public ChangePswActivityPresenter(ChangePswActivityContract.View view, LoginRestClient restClient) {
        mView = view;
        mRestClient = restClient;
    }

    @Override
    public void changePsw(ChangePswParam param) {
        mRestClient.changePsw(param)
                .map(new HttpResultFunc<PhoneRegisterResponse>(RestApiUtils.CHANGE_PSW_API))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<PhoneRegisterResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (mView != null)
                            mView.error(e);
                    }

                    @Override
                    public void onNext(PhoneRegisterResponse phoneRegisterResponse) {
                        if (mView != null)
                          mView.changePswSuccess();
                    }
                });
    }

    @Override
    public void start() {

    }
}
